package com.quanquan.user.service.autoCompute

import java.util.{List, Map}

import com.quanquan.user.service.autoCompute.constant.{SourceTableConst, SymbolConst, TypeConst}
import com.quanquan.user.service.autoCompute.entity.MemberLabel
import com.quanquan.user.service.autoCompute.enums.OptionType
import utils.StringUtils

import scala.collection.JavaConverters._

/**
  * @Auth Gz
  * @CreateTime 2019-07-09
  * @Description
  * scala script for assemble sql that decrease Java development
  */
object SQLHandler {
  /**
    * 拼接同步标签包含人数的sql
    */
  def assembleLabelCount(memberLabel: MemberLabel): String = {
    val table = memberLabel.getLabelFrom
    val optionType = memberLabel.getOptionType
    val sql = new StringBuilder
    table match {
      case SourceTableConst.MEMBER =>
        sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE)
          .append(SymbolConst.COUNT).append(SymbolConst.LBRACKET).append(SymbolConst.MUL)
          .append(SymbolConst.RBRACKET).append(SymbolConst.SPACE).append(SymbolConst.FROM)
          .append(SymbolConst.SPACE).append(memberLabel.getTableName).append(SymbolConst.SPACE)
          .append(SymbolConst.WHERE).append(SymbolConst.SPACE).append(1).append(SymbolConst.EQ)
          .append(1).append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
          .append(memberLabel.getLabelRule)
      case SourceTableConst.CONSUME =>
        if (optionType.equals(OptionType.FREQ)) {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(1).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(SymbolConst.LBRACKET)
            .append(memberLabel.getTempTable).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.SPACE).append(SymbolConst.WHERE)
            .append(SymbolConst.SPACE).append(memberLabel.getTempName).append(SymbolConst.POINT)
            .append("num").append(SymbolConst.SPACE).append(memberLabel.getLeftSymbol).append(memberLabel.getLeftValue)
            .append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.POINT).append("num").append(SymbolConst.SPACE)
            .append(memberLabel.getRightSymbol).append(memberLabel.getRightValue)
        } else {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(SymbolConst.DISTINCT).append(SymbolConst.SPACE)
            .append("user_id").append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.AS).append(SymbolConst.SPACE).append("count").append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(memberLabel.getTableName)
            .append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
            .append(1).append(SymbolConst.EQ).append(1).append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE).append(memberLabel.getLabelRule)
        }
      case SourceTableConst.INTEGRAL =>
        if (optionType.equals(OptionType.FREQ)) {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(1).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(SymbolConst.LBRACKET)
            .append(memberLabel.getTempTable).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.SPACE).append(SymbolConst.WHERE)
            .append(SymbolConst.SPACE).append(memberLabel.getTempName).append(SymbolConst.POINT)
            .append("num").append(SymbolConst.SPACE).append(memberLabel.getLeftSymbol).append(memberLabel.getLeftValue)
            .append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.POINT).append("num").append(SymbolConst.SPACE)
            .append(memberLabel.getRightSymbol).append(memberLabel.getRightValue)
        } else {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(SymbolConst.DISTINCT).append(SymbolConst.SPACE)
            .append("user_id").append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.AS).append(SymbolConst.SPACE).append("count").append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(memberLabel.getTableName)
            .append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
            .append(1).append(SymbolConst.EQ).append(1).append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE).append(memberLabel.getLabelRule)
        }
      case SourceTableConst.PARK =>
        if (optionType.equals(OptionType.FREQ)) {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(1).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(SymbolConst.LBRACKET)
            .append(memberLabel.getTempTable).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.SPACE).append(SymbolConst.WHERE)
            .append(SymbolConst.SPACE).append(memberLabel.getTempName).append(SymbolConst.POINT)
            .append("num").append(SymbolConst.SPACE).append(memberLabel.getLeftSymbol).append(memberLabel.getLeftValue)
            .append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.POINT).append("num").append(SymbolConst.SPACE)
            .append(memberLabel.getRightSymbol).append(memberLabel.getRightValue)
        } else {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(SymbolConst.DISTINCT).append(SymbolConst.SPACE)
            .append("user_id").append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.AS).append(SymbolConst.SPACE).append("count").append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(memberLabel.getTableName)
            .append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
            .append(1).append(SymbolConst.EQ).append(1).append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE).append(memberLabel.getLabelRule)
        }
      case SourceTableConst.COUPON =>
        if (optionType.equals(OptionType.FREQ)) {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(1).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(SymbolConst.LBRACKET)
            .append(memberLabel.getTempTable).append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.SPACE).append(SymbolConst.WHERE)
            .append(SymbolConst.SPACE).append(memberLabel.getTempName).append(SymbolConst.POINT)
            .append("num").append(SymbolConst.SPACE).append(memberLabel.getLeftSymbol).append(memberLabel.getLeftValue)
            .append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
            .append(memberLabel.getTempName).append(SymbolConst.POINT).append("num").append(SymbolConst.SPACE)
            .append(memberLabel.getRightSymbol).append(memberLabel.getRightValue)
        } else {
          sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(SymbolConst.COUNT)
            .append(SymbolConst.LBRACKET).append(SymbolConst.DISTINCT).append(SymbolConst.SPACE)
            .append("user_id").append(SymbolConst.RBRACKET).append(SymbolConst.SPACE)
            .append(SymbolConst.AS).append(SymbolConst.SPACE).append("count").append(SymbolConst.SPACE)
            .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(memberLabel.getTableName)
            .append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
            .append(1).append(SymbolConst.EQ).append(1).append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE).append(memberLabel.getLabelRule)
        }
    }
    return sql.toString
  }

  def assembleTmpTable(fieldName: String, tableName: String): String = {
    val sql = new StringBuilder
    sql.append(SymbolConst.SELECT).append(SymbolConst.SPACE).append(fieldName).append(SymbolConst.COMMA)
      .append(SymbolConst.COUNT).append(SymbolConst.LBRACKET).append(1).append(SymbolConst.RBRACKET)
      .append(SymbolConst.SPACE).append(SymbolConst.AS).append(SymbolConst.SPACE).append("num").append(SymbolConst.SPACE)
      .append(SymbolConst.FROM).append(SymbolConst.SPACE).append(tableName).append(SymbolConst.SPACE)
      .append("WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(cosume_day) GROUP BY user_id")
    return sql.toString
  }

  /**
    * 拼接标签组数量查询sql
    *
    * @param metaData
    * @return
    */
  def assembleGroupCount(metaData: Map[String, List[MemberLabel]]): String = {
    val prefix = new StringBuilder
    val suffix = new StringBuilder
    val data = metaData.asScala
    prefix.append("SELECT COUNT(DISTINCT a.id) AS count FROM oo_user a")
    var res, str1, str2, str3, str4 = ""
    data.foreach(entry =>
      entry._1 match {
        case SourceTableConst.CONSUME =>
          str1 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "b")
        case SourceTableConst.INTEGRAL =>
          str2 = assembleSlaveListWithAlias2(entry._2, prefix, suffix, "c")
        case SourceTableConst.PARK =>
          str3 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "d")
        case SourceTableConst.COUPON =>
          str4 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "e")
        case _ =>
          prefix.append("")
      }
    )
    val end = str1 + str2 + str3 + str4
    val memberLabels = metaData.get(SourceTableConst.MEMBER)
    if (memberLabels != null && !memberLabels.isEmpty) {
      suffix.append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
      assembleMainListWithAlias(memberLabels, suffix, "a");
      res = prefix.append(suffix).toString
      if (end != "") {
        res = res.concat(SymbolConst.SPACE).concat(SymbolConst.AND).concat(end)
      }
    } else {
      res = prefix.append(suffix).toString
      if (end != "") {
        res = res.concat(SymbolConst.SPACE).concat(SymbolConst.WHERE).concat(end)
      }
    }
    return res
  }

  /**
    * 拼装用户画像的sql（带限制条件）
    *
    * @param metaData 表名作为键，对应标签集合作为值
    * @return
    */
  def assembleGroup(metaData: Map[String, List[MemberLabel]], offset: Int, size: Int, isAll: Boolean): String = {
    val prefix = new StringBuilder
    val suffix = new StringBuilder
    var res, str1, str2, str3, str4 = ""
    if (isAll) {
      res = "SELECT * FROM oo_user"
    } else {
      val data = metaData.asScala
      prefix.append("SELECT DISTINCT(a.id) FROM oo_user a")
      if (data == null || data.isEmpty) {
        return prefix.toString
      }
      data.foreach(entry =>
        entry._1 match {
          case SourceTableConst.CONSUME =>
            str1 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "b")
          case SourceTableConst.INTEGRAL =>
            str2 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "c")
          case SourceTableConst.PARK =>
            str3 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "d")
          case SourceTableConst.COUPON =>
            str4 = assembleSlaveListWithAlias(entry._2, prefix, suffix, "e")
          case _ =>
            prefix.append("")
        }
      )
      val end = str1 + str2 + str3 + str4
      val memberLabels = metaData.get(SourceTableConst.MEMBER)
      if (memberLabels != null && !memberLabels.isEmpty) {
        suffix.append(SymbolConst.SPACE).append(SymbolConst.WHERE).append(SymbolConst.SPACE)
        assembleMainListWithAlias(memberLabels, suffix, "a");
        res = prefix.append(suffix).toString
        if (end != "") {
          res = res.concat(SymbolConst.SPACE).concat(SymbolConst.AND).concat(end)
        }
      } else {
        res = prefix.append(suffix).toString
        if (end != "") {
          res = res.concat(SymbolConst.SPACE).concat(SymbolConst.WHERE).concat(end)
        }
      }
    }
    res = res.concat(SymbolConst.SPACE).concat(SymbolConst.LIMIT).concat(SymbolConst.SPACE)
      .concat(offset.toString).concat(SymbolConst.COMMA).concat(size.toString)
    return res
  }

  private def assembleSlaveListWithAlias(labels: List[MemberLabel], prefix: StringBuilder, suffix: StringBuilder, alias: String): String = {
    val end = new StringBuilder
    var isFirst = true
    for (label <- labels.asScala) {
      if (OptionType.SPAN.equals(label.getOptionType)) {
        if (isFirst) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.INNER_JOIN).append(SymbolConst.SPACE)
            .append(label.getTableName).append(SymbolConst.SPACE).append(alias).append(SymbolConst.SPACE)
            .append(SymbolConst.ON).append(SymbolConst.SPACE).append("a.id").append(SymbolConst.EQ)
            .append(alias).append(SymbolConst.POINT).append("user_id").append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE)
          isFirst = false
        }
        val dateRange = assembleDateRangeWithAlias(label.getFieldName, label.getOption, alias)
        prefix.append(dateRange);
        if (labels.indexOf(label) + 1 != labels.size &&
          !OptionType.FREQ.equals(labels.asScala.last.getOptionType)) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
        }
      } else if (OptionType.FREQ.equals(label.getOptionType)) {
        suffix.append(SymbolConst.COMMA).append(SymbolConst.LBRACKET)
          .append(label.getTempTable).append(SymbolConst.RBRACKET)
          .append(SymbolConst.SPACE).append(label.getTempName)
        end.append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append("num").append(SymbolConst.SPACE).append(label.getLeftSymbol).append(SymbolConst.SPACE)
          .append(label.getLeftValue).append(SymbolConst.SPACE).append(SymbolConst.AND)
          .append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append("num").append(SymbolConst.SPACE).append(label.getRightSymbol).append(SymbolConst.SPACE)
          .append(label.getRightValue).append(SymbolConst.SPACE).append(SymbolConst.AND)
          .append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append(label.getFieldName).append(SymbolConst.SPACE).append(SymbolConst.EQ)
          .append(SymbolConst.SPACE).append("a.id")
      } else {
        if (isFirst) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.INNER_JOIN).append(SymbolConst.SPACE)
            .append(label.getTableName).append(SymbolConst.SPACE).append(alias).append(SymbolConst.SPACE)
            .append(SymbolConst.ON).append(SymbolConst.SPACE).append("a.id").append(SymbolConst.EQ)
            .append(alias).append(SymbolConst.POINT).append("user_id").append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE)
          isFirst = false
        }
        var leftSymbol = label.getLeftSymbol
        if (SymbolConst.LT.equals(leftSymbol)) leftSymbol = SymbolConst.GT
        if (SymbolConst.L_E.equals(leftSymbol)) leftSymbol = SymbolConst.G_E
        val leftExp = assembleWithAlias(label.getFieldType, label.getFieldName, leftSymbol, label.getLeftValue, alias)
        val rightExp = assembleWithAlias(label.getFieldType, label.getFieldName, label.getRightSymbol, label.getRightValue, alias)
        if (StringUtils.isNotEmpty(leftExp) && StringUtils.isNotEmpty(rightExp)) {
          prefix.append(leftExp).append(SymbolConst.SPACE).append(SymbolConst.AND)
            .append(SymbolConst.SPACE).append(rightExp)
        } else {
          prefix.append(leftExp).append(rightExp)
        }
        if (labels.indexOf(label) + 1 != labels.size && !OptionType.FREQ.equals(labels.asScala.last.getOptionType)) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
        }
      }
    }
    return end.toString
  }

  private def assembleSlaveListWithAlias2(labels: List[MemberLabel], prefix: StringBuilder, suffix: StringBuilder, alias: String): String = {
    val end = new StringBuilder
    var isFirst = true
    for (label <- labels.asScala) {
      if (OptionType.SPAN.equals(label.getOptionType)) {
        if (isFirst) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.INNER_JOIN).append(SymbolConst.SPACE)
            .append(label.getTableName).append(SymbolConst.SPACE).append(alias).append(SymbolConst.SPACE)
            .append(SymbolConst.ON).append(SymbolConst.SPACE).append("a.id").append(SymbolConst.EQ)
            .append(alias).append(SymbolConst.POINT).append("user_id").append(SymbolConst.SPACE)
            .append(SymbolConst.AND).append(SymbolConst.SPACE)
          isFirst = false
        }
        val dateRange = assembleDateRangeWithAlias(label.getFieldName, label.getOption, alias)
        prefix.append(dateRange);
        if (labels.indexOf(label) + 1 != labels.size &&
          !OptionType.FREQ.equals(labels.asScala.last.getOptionType)) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
        }
      } else if (OptionType.FREQ.equals(label.getOptionType)) {
        suffix.append(SymbolConst.COMMA).append(SymbolConst.LBRACKET)
          .append(label.getTempTable).append(SymbolConst.RBRACKET)
          .append(SymbolConst.SPACE).append(label.getTempName)
        end.append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append("num").append(SymbolConst.SPACE).append(label.getLeftSymbol).append(SymbolConst.SPACE)
          .append(label.getLeftValue).append(SymbolConst.SPACE).append(SymbolConst.AND)
          .append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append("num").append(SymbolConst.SPACE).append(label.getRightSymbol).append(SymbolConst.SPACE)
          .append(label.getRightValue).append(SymbolConst.SPACE).append(SymbolConst.AND)
          .append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.POINT)
          .append(label.getFieldName).append(SymbolConst.SPACE).append(SymbolConst.EQ)
          .append(SymbolConst.SPACE).append("a.id")
      } else {
        if (isFirst) {
          prefix.append(" inner join ")
            .append("select user_id,sum(").append(label.getFieldName).append(") from ").append(label.getTableName).append(" ").append(alias).append(" ")
            .append("on a.id =").append(alias).append(".user_id and ")
          isFirst = false
        }
        var leftSymbol = label.getLeftSymbol
        if (SymbolConst.LT.equals(leftSymbol)) leftSymbol = SymbolConst.GT
        if (SymbolConst.L_E.equals(leftSymbol)) leftSymbol = SymbolConst.G_E
        val leftExp = assembleWithAlias(label.getFieldType, label.getFieldName, leftSymbol, label.getLeftValue, alias)
        val rightExp = assembleWithAlias(label.getFieldType, label.getFieldName, label.getRightSymbol, label.getRightValue, alias)
        if (StringUtils.isNotEmpty(leftExp) && StringUtils.isNotEmpty(rightExp)) {
          prefix.append(leftExp).append(SymbolConst.SPACE).append(SymbolConst.AND)
            .append(SymbolConst.SPACE).append(rightExp)
        } else {
          prefix.append(leftExp).append(rightExp)
        }
        if (labels.indexOf(label) + 1 != labels.size && !OptionType.FREQ.equals(labels.asScala.last.getOptionType)) {
          prefix.append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
        }
      }
    }
    return end.toString
  }


  private def assembleMainListWithAlias(labels: List[MemberLabel], suffix: StringBuilder, alias: String): Unit = {
    for (label <- labels.asScala) {
      if (OptionType.SPAN.equals(label.getOptionType)) {
        val dateRange = assembleDateRangeWithAlias(label.getFieldName, label.getOption, alias)
        suffix.append(dateRange);
      } else if (OptionType.FREQ.equals(label.getOptionType) && label.getUseTmp) {
        suffix.append(SymbolConst.COMMA).append(SymbolConst.LBRACKET)
          .append(label.getTempTable).append(SymbolConst.RBRACKET)
          .append(SymbolConst.SPACE).append(label.getTempName).append(SymbolConst.SPACE)
      } else {
        var leftSymbol = label.getLeftSymbol
        if (SymbolConst.LT.equals(leftSymbol)) leftSymbol = SymbolConst.GT
        if (SymbolConst.L_E.equals(leftSymbol)) leftSymbol = SymbolConst.G_E
        val leftExp = assembleWithAlias(label.getFieldType, label.getFieldName, leftSymbol, label.getLeftValue, alias)
        val rightExp = assembleWithAlias(label.getFieldType, label.getFieldName, label.getRightSymbol, label.getRightValue, alias)
        if (StringUtils.isNotEmpty(leftExp) && StringUtils.isNotEmpty(rightExp)) {
          suffix.append(leftExp).append(SymbolConst.SPACE).append(SymbolConst.AND)
            .append(SymbolConst.SPACE).append(rightExp)
        } else {
          suffix.append(leftExp).append(rightExp)
        }
      }
      if (labels.indexOf(label) + 1 != labels.size) {
        suffix.append(SymbolConst.SPACE).append(SymbolConst.AND).append(SymbolConst.SPACE)
      }
    }
  }

  private def assembleWithAlias(fieldType: String, field: String, symbol: String, value: String, alias: String): String = {
    val sql = new StringBuilder
    if (StringUtils.isEmpty(value) || StringUtils.isEmpty(symbol))
      return sql.toString
    sql.append(alias).append(SymbolConst.POINT)
    if (TypeConst.INT == fieldType) {
      sql.append(field)
        .append(SymbolConst.SPACE)
        .append(symbol)
        .append(SymbolConst.SPACE)
        .append(value)
    } else {
      if (SymbolConst.LIKE.eq(symbol))
        sql.append(field)
          .append(SymbolConst.SPACE)
          .append(symbol)
          .append(SymbolConst.SPACE)
          .append(SymbolConst.QUA)
          .append(SymbolConst.PER)
          .append(value)
          .append(SymbolConst.PER)
          .append(SymbolConst.QUA)
      else
        sql.append(field)
          .append(SymbolConst.SPACE)
          .append(symbol)
          .append(SymbolConst.SPACE)
          .append(SymbolConst.QUA)
          .append(value)
          .append(SymbolConst.QUA)
    }
    return sql.toString
  }

  private def assembleDateRangeWithAlias(field: String, symbol: String, alias: String): String = {
    val sql = new StringBuilder
    if (StringUtils.isEmpty(field) || StringUtils.isEmpty(symbol))
      return sql.toString
    sql.append(symbol)
      .append(SymbolConst.LBRACKET)
      .append(alias).append(SymbolConst.POINT)
      .append(field)
      .append(SymbolConst.RBRACKET)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.EQ)
      .append(SymbolConst.SPACE)
      .append(symbol)
      .append(SymbolConst.LBRACKET)
      .append(SymbolConst.CURDATE)
      .append(SymbolConst.LBRACKET)
      .append(SymbolConst.RBRACKET)
      .append(SymbolConst.RBRACKET)
    return sql.toString
  }


  //拼接单值标签
  def assembleSingle(field: String, symbol: String, value: String): String = {
    val sql = new StringBuilder
    if (SymbolConst.LIKE.eq(symbol)) //拼接like语句
      sql.append(field)
        .append(SymbolConst.SPACE)
        .append(symbol)
        .append(SymbolConst.SPACE)
        .append(SymbolConst.QUA)
        .append(SymbolConst.PER)
        .append(value)
        .append(SymbolConst.PER)
        .append(SymbolConst.QUA)
    else //非like语句
      sql.append(field)
        .append(SymbolConst.SPACE)
        .append(symbol)
        .append(SymbolConst.SPACE)
        .append(SymbolConst.QUA)
        .append(value)
        .append(SymbolConst.QUA)
    return sql.toString
  }

  //拼接日期范围（年、季、月、周、日）
  def assembleDateRange(field: String, symbol: String): String = {
    val sql = new StringBuilder()
      .append(symbol)
      .append(SymbolConst.LBRACKET)
      .append(field)
      .append(SymbolConst.RBRACKET)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.EQ)
      .append(SymbolConst.SPACE)
      .append(symbol)
      .append(SymbolConst.LBRACKET)
      .append(SymbolConst.CURDATE)
      .append(SymbolConst.LBRACKET)
      .append(SymbolConst.RBRACKET)
      .append(SymbolConst.RBRACKET)
    return sql.toString
  }

  //拼接单值标签
  def assembleStrNum(field: String, symbol: String, value: String): String = {
    val sql = new StringBuilder
    if (SymbolConst.LIKE.eq(symbol))
      return sql.toString
    else
      sql.append(field)
        .append(SymbolConst.SPACE)
        .append(symbol)
        .append(SymbolConst.SPACE)
        .append(value)
    return sql.toString
  }

  //拼接二值标签
  def assembleDouble(leftValue: String, leftSymbol: String, field: String, rightSymbol: String, rightValue: String): String = {
    val sql = new StringBuilder
    sql.append(field)
      .append(SymbolConst.SPACE)
      .append(leftSymbol)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.QUA)
      .append(leftValue)
      .append(SymbolConst.QUA)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.AND)
      .append(SymbolConst.SPACE)
      .append(field)
      .append(SymbolConst.SPACE)
      .append(rightSymbol)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.QUA)
      .append(rightValue)
      .append(SymbolConst.QUA)
    return sql.toString
  }

  //拼接String/Number类型的二值标签
  def assembleStrNum(leftValue: String, leftSymbol: String, field: String, rightSymbol: String, rightValue: String): String = {
    val sql = new StringBuilder
    sql.append(field)
      .append(SymbolConst.SPACE)
      .append(leftSymbol)
      .append(SymbolConst.SPACE)
      .append(leftValue)
      .append(SymbolConst.SPACE)
      .append(SymbolConst.AND)
      .append(SymbolConst.SPACE)
      .append(field)
      .append(SymbolConst.SPACE)
      .append(rightSymbol)
      .append(SymbolConst.SPACE)
      .append(rightValue)
    return sql.toString
  }
}
